package tq.com.tqstudent.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.common.utils.FileUtil;
import com.lib.common.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.ChatBean;
import tq.com.tqstudent.ui.activity.classes.VideoPlayActivity;
import tq.com.tqstudent.utils.VarietyUtil;
import tq.com.tqstudent.widgets.PreviewImgActivity;

/**
 * @Author rames
 * @date 2019/3/14 15:45
 */
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<ChatBean> list;//数据源
    private Context context;//上下文

    public ChatAdapter(List<ChatBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void addData(ChatBean bean){
        this.list.add(bean);
        this.notifyDataSetChanged();
    }

    public void setDatas(List<ChatBean> list){
        this.list = list;
        this.notifyDataSetChanged();
    }

    public List<ChatBean> getDatas(){
        return list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //选择类型
        switch (viewType){
            // 对方的文本内容item
            case ChatBean.TYPE_OPPOSITE_TEXT:
                View viewOne = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_text,parent,false);
                return new TextViewHolder(viewOne);

                // 我方的图片item
            case ChatBean.TYPE_MINE_IMAGE:
                View viewTwo = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_img_mine,parent,false);
                return new ImageMineViewHolder(viewTwo);

            // 对方的图片内容
            case ChatBean.TYPE_OPPOSITE_IMAGE:
                View viewThree = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_img, parent,false);
                return new ImageHolder(viewThree);

            // 我方的文本内容
            case ChatBean.TYPE_MINE_TEXT:
                View viewFour = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_text_mine, parent,false);
                return new TextMineHolder(viewFour);

            // 对方的视频播放
            case ChatBean.TYPE_OPPOSITE_VIDEO:
                View viewVideo = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_video, parent,false);
                return new VideoHolder(viewVideo);

        }
        return null;
    }

    //绑定
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ChatBean item = list.get(position);
        // 对方的文本内容item
        if(holder instanceof TextViewHolder){
            TextViewHolder view = ((TextViewHolder)holder);
            view.contentTV.setText(item.message);
            view.nameTV.setText(initName(item.username));
            Glide.with(context).load(VarietyUtil.getHeadImgUri(item.icon)).apply(VarietyUtil.getGlideHeadOptions()).into(view.headIV);

            // 我方的图片item
        } else if(holder instanceof ImageMineViewHolder){
            ImageMineViewHolder view = (ImageMineViewHolder) holder;
            view.nameTV.setText(initName(item.username));
            Glide.with(context).load(VarietyUtil.getHeadImgUri(item.icon)).apply(VarietyUtil.getGlideHeadOptions()).into(view.headIV);

            if(!TextUtils.isEmpty(item.imageID)){
                Glide.with(context).load(App.getFileUrl(item.imageID)).into(view.imageIV);
            } else {
                view.imageIV.setImageURI(item.imagePath);
            }

            view.imageIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(!TextUtils.isEmpty(item.imageID)){
                        PreviewImgActivity.show((Activity) context, App.getFileUrl(item.imageID), 0);
                    } else {
                        PreviewImgActivity.show((Activity) context, FileUtil.getFilePathByUri(context, item.imagePath), 0);
                    }

                }
            });

            // 对方的图片内容
        }else if(holder instanceof ImageHolder){
            ImageHolder view = (ImageHolder) holder;
            view.nameTV.setText(initName(item.username));
            Glide.with(context).load(VarietyUtil.getHeadImgUri(item.icon)).apply(VarietyUtil.getGlideHeadOptions()).into(view.headIV);

            if(!TextUtils.isEmpty(item.imageID)){
                Glide.with(context).load(App.getFileUrl(item.imageID)).into(view.imageIV);
            } else {
                view.imageIV.setImageURI(item.imagePath);
            }

            view.imageIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArrayList<String> list = new ArrayList<>();
                    list.add(App.getFileUrl(item.imageID));
                    PreviewImgActivity.show((Activity) context, list, 0, 0);
                }
            });

            // 我方的文本内容
        }else if(holder instanceof TextMineHolder){
            TextMineHolder view = (TextMineHolder) holder;
            view.contentTV.setText(item.message);
            view.nameTV.setText(initName(item.username));
            Glide.with(context).load(VarietyUtil.getHeadImgUri(item.icon)).apply(VarietyUtil.getGlideHeadOptions()).into(view.headIV);

            // 对方的视频Holder
        } else if(holder instanceof VideoHolder){
            VideoHolder view = (VideoHolder) holder;
            view.nameTV.setText(initName(item.username));
            Glide.with(context).load(VarietyUtil.getHeadImgUri(item.icon)).apply(VarietyUtil.getGlideHeadOptions()).into(view.headIV);

//            if(!TextUtils.isEmpty(item.imageID)){
//                Glide.with(context).load(App.getFileUrl(item.imageID)).into(view.imageIV);
//            } else {
//                view.imageIV.setImageURI(item.imagePath);
//            }

            view.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    VideoPlayActivity.show((Activity) context, item.matid, item.attid, item.title, App.getFileUrl(item.videoUrl), 0);
                }
            });
        }
    }

    private String initName(String name){
        if(TextUtils.isEmpty(name)){
            return "路人甲";
        } else {
            return name;
        }
    }

    //有多少个item？
    @Override
    public int getItemCount() {
        return null==list ? 0 : list.size();
    }

    //item类型
    @Override
    public int getItemViewType(int position) {
        if(null == list){
            LogUtil.e("list is null");
            return 1;
        }
        if(null == list.get(position)){
            LogUtil.e("list.get(position) is null");
            return 1;
        }
        return list.get(position).type;
    }

    //=======================以下为item中的button控件点击事件处理===================================

    //第一步：自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener  {
        void onItemClick(View v, int position);
        void onItemLongClick(View v, int position);
    }

    public OnItemClickListener mOnItemClickListener;//第二步：声明自定义的接口

    //第三步：定义方法并暴露给外面的调用者
    public void setOnItemClickListener(OnItemClickListener  listener) {
        this.mOnItemClickListener  = listener;
    }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //第一个item类型,对方的文本内容
    class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        private ImageView headIV;
        private TextView nameTV, contentTV;

        public TextViewHolder(View itemView) {
            super(itemView);
            headIV = itemView.findViewById(R.id.head_iv);
            nameTV = itemView.findViewById(R.id.name_tv);
            contentTV = itemView.findViewById(R.id.content_tv);
            // 为item及item内部控件添加点击事件
            itemView.setOnClickListener(this);
            headIV.setOnLongClickListener(this);
        }
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View view) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemLongClick(view, getAdapterPosition());
            }
            return false;
        }
    }

    // 第三个item类型, 对方的图片内容
    class ImageHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        private ImageView headIV, imageIV;
        private TextView nameTV;

        public ImageHolder(@NonNull View itemView) {
            super(itemView);
            headIV = itemView.findViewById(R.id.head_iv);
            nameTV = itemView.findViewById(R.id.name_tv);
            imageIV = itemView.findViewById(R.id.image);
            // 为item添加点击事件
            itemView.setOnClickListener(this);
            headIV.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View view) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemLongClick(view, getAdapterPosition());
            }
            return false;
        }
    }

    //第二个item类型,我方的图片内容
    class ImageMineViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView headIV, imageIV;
        private TextView nameTV;

        public ImageMineViewHolder(View itemView) {
            super(itemView);
            headIV = itemView.findViewById(R.id.head_iv);
            nameTV = itemView.findViewById(R.id.name_tv);
            imageIV = itemView.findViewById(R.id.image);
            // 为item添加点击事件
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

    //第四个item类型,我方的文本内容
    class TextMineHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView headIV;
        private TextView nameTV, contentTV;

        public TextMineHolder(View itemView) {
            super(itemView);
            headIV = itemView.findViewById(R.id.head_iv);
            nameTV = itemView.findViewById(R.id.name_tv);
            contentTV = itemView.findViewById(R.id.content_tv);
            // 为item及item内部控件添加点击事件
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

    //第五个item类型,对方的视频内容
    class VideoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView headIV, imageIV;
        private TextView nameTV;
        private RelativeLayout layout;

        public VideoHolder(View itemView) {
            super(itemView);
            headIV = itemView.findViewById(R.id.head_iv);
            nameTV = itemView.findViewById(R.id.name_tv);
            imageIV = itemView.findViewById(R.id.image);
            layout = itemView.findViewById(R.id.layout);
            // 为item及item内部控件添加点击事件
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
